/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import controller.EventManager;
import controller.InvitationManager;
import controller.UserManager;
import entity.Event;
import entity.Invitation;
import entity.User;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.ViewScoped;
import utility.Forecast;


/**
 *
 * @author xubuntu
 */
@ManagedBean
@ViewScoped
public class EventBean {    
    @ManagedProperty( value = "#{personalPageBean}" )
    private PersonalPageBean personalPageBean;
    @ManagedProperty( value = "#{loginBean}" )
    private LoginBean loginBean;
    @ManagedProperty( value = "#{weatherBean}")
    private WeatherBean weatherBean;
    @ManagedProperty( value = "#{navigationBean}" )
    private NavigationBean navigationBean;

    @EJB
    private EventManager evtMgr;
    @EJB
    private UserManager usrMgr;
    @EJB
    private InvitationManager ivtMgr;
    
    private Forecast meteo;
    private String meteoImg;
    private Event event;
    private String going="Going?";
    
    @PostConstruct
    public void init(){
        going="Going?";
        event = personalPageBean.getEventSelected();
        int outdoor=0;
        
        if(loginBean.getWeathers().isEmpty()==false){
            for(Forecast f: loginBean.getWeathers()){
                if((f.getPlace().equalsIgnoreCase(event.getPlace())== true)&&(f.getDates().getDate() == event.getDateStart().getDate())){
                    meteo=f;
                    outdoor=1;
                    break;
                }
            }
        }
        if(outdoor==0){
            meteo = new Forecast("Not available","Indoor event or not available yet","Not available");
        }
        
        meteoImg = weatherBean.getImageWeather(meteo.getMeteo());
        
    }
    
    
    public String deleteEvent(){
    
        System.out.println("Sto per cancellare l'evento"+event.getName());
        int i = 0;
        while( i < loginBean.getEventsTakePart().size() )
        {
            if( loginBean.getEventsTakePart().get( i ).equals(event)== true){
                loginBean.getEventsTakePart().remove( i );
            }   
            else
                i++;
        }
        
        if(event.getInvitationCollection()!= null && event.getInvitationCollection().size()>0){ 
            System.out.println("vent.getInvitationCollection().size():= "+event.getInvitationCollection().size());
     
            event.getInvitationCollection().clear();
            System.out.println("dopo clear event.getInvitationCollection().size():= "+event.getInvitationCollection().size());
            
        }
        if(evtMgr.cancelEventDb(event)== true){
            System.out.println("Evento rimosso con successo dal db: "+event.getName());
     }
     return navigationBean.redirectToPersonalPage();
    }
    
    
    public String changeGoing(){
        System.out.println("Sono nel changeGoing");
        Invitation invito=null;
        List<Invitation> invitations = ivtMgr.getAllInvitationUser(loginBean.getUser());
        if(invitations.isEmpty()== false){
            for(Invitation i : invitations){
                if(i.getEvent().equals(event))
                    invito=i;
            }
            if(invito.getAnswer()==0){
                invito.setAnswer(1);
                loginBean.getEventsTakePart().add(event);
            }else
                if(invito.getAnswer()==1){
                    invito.setAnswer(2);
                    loginBean.getEventsTakePart().remove(event);
                    personalPageBean.getEventDeclined().add(event);
                    System.out.println("risposta ad invito modificata: "+ invito.getAnswer());
                }else{
                invito.setAnswer(1);
                personalPageBean.getEventDeclined().remove(event);
                loginBean.getEventsTakePart().add(event);
                System.out.println("risposta ad invito modificata: "+ invito.getAnswer());
                }

            if(ivtMgr.UpdateInvitation(invito)== true)
            { 
                
                if(invito.getAnswer()== 1)
                    return going = "Going!";
                    
                else if(invito.getAnswer() == 2)
                    return going ="Not Going";
                  
                
            }
            else
                System.out.println("risposta ad invito non modificata");
        }
        return navigationBean.redirectToEventPage();
    }
    
 
    
    public Event getEvent() {
        return event;
    }

    public void setEvent(Event event) {
        this.event = event;
    }

    public WeatherBean getWeatherBean() {
        return weatherBean;
    }

    public void setWeatherBean(WeatherBean weatherBean) {
        this.weatherBean = weatherBean;
    }

    public Forecast getMeteo() {
        return meteo;
    }

    public void setMeteo(Forecast meteo) {
        this.meteo = meteo;
    }

    public String getMeteoImg() {
        return meteoImg;
    }

    public void setMeteoImg(String meteoImg) {
        this.meteoImg = meteoImg;
    }

    public LoginBean getLoginBean() {
        return loginBean;
    }

    public void setLoginBean(LoginBean loginBean) {
        this.loginBean = loginBean;
    }

   public boolean visibility(){
       if(loginBean.getUser().equals(event.getOrganizer())&& (event.getDateStart().before(new Date())== false))
           return true;
       return false;
   }
   public String areYouInvited(){
       int presente=0;
       Invitation invito=null;
        List<Invitation> invitations = ivtMgr.getAllInvitationUser(loginBean.getUser());
        if(invitations.isEmpty()== false){
            for(Invitation i : invitations){
                if(i.getEvent().equals(event)){
                    presente=1;
                    break;
                }
            }
        }
       /*for(Invitation i: event.getInvitationCollection()){
           if(loginBean.getUser().equals(i.getUser())){
               presente=1;
               break;
           }
       }*/
        if(event.getInvitationCollection()!=null){
            System.out.println("dimensione inviti: "+event.getInvitationCollection().size());
            System.out.println("presente=  "+presente);
            Date today = new Date();
            if((loginBean.getUser().getNickname().equals(event.getOrganizer().getNickname())==false)&&(presente == 1)&&(event.getDateStart().before(today)== false) )
                    return "true";
            
        }
        return "false";
   }

    public PersonalPageBean getPersonalPageBean() {
        return personalPageBean;
    }

    public void setPersonalPageBean(PersonalPageBean personalPageBean) {
        this.personalPageBean = personalPageBean;
    }

    public NavigationBean getNavigationBean() {
        return navigationBean;
    }

    public void setNavigationBean(NavigationBean navigationBean) {
        this.navigationBean = navigationBean;
    }

    public String getGoing() {
        return going;
    }

    

    
}
